<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>添加日程</title>
    <link rel="stylesheet" type="text/css" href="../../css/mui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/schedule.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/mui.picker.min.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css">
    <link rel="stylesheet" type="text/css" href="../../css/t_style.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/commonality.css">
    <script src="../../js/mui.min.js"></script>
    <script src="../../js/jquery.min.js"></script>
    <script src="../../js/vue.js"></script>
    <script src="../../js/main.js"></script>
    <script src="../../js/jquery.flexText.min.js"></script>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body style="overflow-x: hidden">
<div id="app">
    <header class="mui-bar mui-bar-nav">
        <span class="back" @click="back"></span>
        <span class="cross" @click="exit()"></span>
        <h1 class="mui-title my_mui-title">添加日程</h1>
    </header>
    <div class="mui-content" >
        <div class="title">
            <p>最后保存：{{ time|getData}}</p>
            <div class="clearFix"></div>
        </div>
        <div class="module">
            <div class="mui-input-row">
                <label>标题:</label>
                <!--<input type="text" class="mui-input-clear" v-model="title" placeholder="请输入文字" style="float:left;">-->
                <div class="write-area myWrite-area" id="sch_title" >
                    <textarea v-model="title" placeholder="请输入文字" ></textarea>
                </div>
            </div>
            <div class="mui-input-row">
                <label>内容:</label>
                <div class="write-area myWrite-area" >
                    <textarea type="text" class="mui-input-clear" v-model="message" placeholder="请输入文字"></textarea>
                </div>
            </div>
            <div class="mui-input-row">
                <label>截至时间</label>
                <div id='results' class="ui-alert"></div>
                <button id='demo3' data-options='{"beginYear":2017,"beginMonth":,"beginDay":,endYear":2020}' class="btn mui-btn mui-btn-link addBtn">添加</button>
            </div>
        </div>
        <div class="add-sender add-me ">
            <p class="add-sch-title">指定协作人<span>（唯一）</span></p>
            <div class="fl"  v-for="(item,index) in onlyOneArr">
                <div class="report-headimg choose_people " @click="del_c(index)">
                    {{item.name|getHName}}
                </div>
                <div class="sender" v-text="item.name"></div>
            </div>
            <div  class="fl">
                <div class="add-btn" @click="open_popup(2)"></div>
                <p class="sender" >添加</p>
            </div>
            <div class="clearFix"></div>
        </div>
        <div class="add-sender add-me  ">
            <p class="add-sch-title">抄送人</p>
                <div class="fl" v-for="(item,index) in selected">
                    <div class="report-headimg choose_people " @click="del(index)">
                        {{item.name|getHName}}
                    </div>
                    <div class="sender" v-text="item.name"></div>
                </div>
                <div  class="fl">
                    <div class="add-btn" @click="open_popup(1)"></div>
                    <p class="sender" >添加</p>
                </div>
            <div class="clearFix"></div>
        </div>
        <div class="footer">
            <div class="submit-btn" style="background-color: white">
                <button class="mui-btn mui-btn-primary" style="position:relative;top:-6px" @click="submit" :disabled="isUse">确定发送</button>
            </div>
        </div>
    </div>
    <div class="popup-bg" v-show="dept">
        <header class="mui-bar mui-bar-nav">
            <span class="back" @click="go_back"></span>
            <h1 class="mui-title">选择接收人</h1>
        </header>
        <div class="popup-content">
            <div class="mui-input-row mui-search">
                <span class="mui-icon mui-icon-search" @click="search"></span>
                <input type="text" :class="{shortter:isSearch}" v-model="searchInfo" placeholder="搜索用户名">
                <span class="cancel"  @click="go_back" v-if="isSearch">取消</span>
                <div class="crumbs" v-if="!isSearch">
                    <ul>
                        <li @click="show_info(true, 0)" v-text="companyName">天津中新智冠信息技术有限公司</li>
                        <li v-for="(item, index) in crumbs" @click="show_info(false, index)">&nbsp;>&nbsp;{{item.name}}</li>
                        <div class="clear"></div>
                    </ul>
                </div>
            </div>
            <ul class="popup-list" :class="isSearch?'popup-list-sec':'popup-list-fir'">
                <li v-for="(item, index) in depts">
                    <div class="fl">
                        <input type="checkbox" :checked="item.check" @click="toggle(index)">
                    </div>
                    <div @click="judge(index,1)">
                        <div class="fr popup-next" v-show="item.show">
                            <span>{{item.total}}</span>
                            <img src="../../images/xuanzebumen_right.png" alt="">
                        </div>
                        <div class="popup-info">
                            <div class="report-top" style="padding: 6px 0;float: left;margin-left: 10px">
                                <div class="report-headimg fl" style="margin-top: 2px" v-if="item.show" v-text="item.hName"></div>
                                <div class="report-headimg fl" style="margin-top: 2px" v-else="item.show">{{item.name | getHName}}</div>
                                <div class="report-info">
                                    <p style="line-height: 45px">{{item.name}}</p>
                                </div>
                            </div>
                        </div>
                        <div class="clear"></div>
                    </div>
                </li>
            </ul>
        </div>

        <div class="popup-footer">
            <div class="haveSelected">已选择: <span v-text="num==1?selected_num:checked_num"></span>人</div>
            <div class="popup-footer-btn fr" @click="confirm">
                确定
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="popup-bg" v-show="selectOnly">
        <header class="mui-bar mui-bar-nav">
            <span class="back" @click="go_back"></span>
            <h1 class="mui-title">选择接收人</h1>
        </header>
        <div class="popup-content">
            <div class="mui-input-row mui-search">
                <span class="mui-icon mui-icon-search" @click="search"></span>
                <input type="text" :class="{shortter:isSearch}" v-model="searchInfo" placeholder="搜索用户名">
                <span class="cancel"  @click="go_back" v-if="isSearch">取消</span>
                <div class="crumbs" v-if="!isSearch">
                    <ul>
                        <li style="color: #2c9ae8" @click="show_info(true, 0)" v-text="companyName">天津中新智冠信息技术有限公司</li>
                        <li v-for="(item, index) in crumbs" @click="show_info(false, index)">&nbsp;>&nbsp;{{item.name}}</li>
                        <div class="clear"></div>
                    </ul>
                </div>
            </div>
            <ul class="popup-list" :class="isSearch?'popup-list-sec':'popup-list-fir'">
                <li v-for="(item, index) in depts">
                    <div class="fl">
                        <input type="checkbox" :checked="item.check" v-show="!item.show" @click="toggle_only(index)">
                    </div>
                    <div @click="judge(index,2)">
                        <div class="fr popup-next" v-show="item.show">
                            <span>{{item.total}}</span>
                            <img src="../../images/xuanzebumen_right.png" alt="">
                        </div>
                        <div class="popup-info">
                            <div class="report-top" style="padding: 6px 0;float: left;margin-left: 10px">
                                <div class="report-headimg fl" style="margin-top: 2px" v-if="item.show" v-text="item.hName"></div>
                                <div class="report-headimg fl" style="margin-top: 2px" v-else="item.show">{{item.name | getHName}}</div>
                                <div class="report-info">
                                    <p style="line-height: 45px">{{item.name}}</p>
                                </div>
                            </div>
                        </div>
                        <div class="clear"></div>
                    </div>
                </li>
            </ul>
        </div>

        <div class="popup-footer">
            <div class="haveSelected">已选择: <span v-text="onlyOneArr.length"></span>人</div>
            <div class="popup-footer-btn fr" @click="confirm">
                确定
            </div>
            <div class="clear"></div>
        </div>
    </div>
  </div>
</body>
<script src="../../js/mui.picker.min.js"></script>
<script>
    $(function () {
        $('textarea').flexText();
        var year=new Date().getFullYear() ;
        var  month=new Date().getMonth()+1;
        var day=new Date().getDate();
        var hour=new Date().getHours();
        var minute=new Date().getMinutes();
        $("#demo3").attr("data-options",'{"type":"datatime","beginYear":'+year+',"beginMonth":'+month+',"beginDay":'+day+',"beginHours":'+hour+',"beginMinutes":'+minute+'}');
    });
    function UrlSearch(){
        var name,value;
        var str=location.href; //取得整个地址栏
        var num=str.indexOf("?");
        str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
        var arr=str.split("&"); //各个参数放到数组里
        for(var i=0;i < arr.length;i++){
            num=arr[i].indexOf("=");
            if(num>0){
                name=arr[i].substring(0,num);
                value=arr[i].substr(num+1);
                this[name]=value;
            }
        }
    }
    var Request = new UrlSearch(); //实例化

//        Request.cid=Request.cid==null?6:Request.cid;
        function sendInfoToJava(name) {
            //调用android程序中的方法，并传递参数
            javascript:WebView.showInfoFromJs(name);
        }
        function zeroize( num ) {
            return (String(num).length == 1 ? '0' : '') + num;
        }
        var time;
        var app = new Vue({
            el: "#app",
            data: function () {
                return{
                    popup: false,
                    title: "",
                    message:"",
    //                sendUserId:16,
                    sendUser:"loginName",
                    createUser:"",
                    ccUserName:[],
                    companyId:0,
                    time:"",
                    depts: [],
                    users: [],
                    dept: false,
                    user: false,
                    selected: [],
                    onlyOneArr:[],
                    selectOnly:false,
                    temp:[],
                    userType:"",
                    selected_num: 0,
                    checked_num: 0,
                    checked: [],
                    rid: [],
                    num: 1,
                    select_person: [],
                    check_person: [],
                    //                    **********搜索
                    searchInfo:"",
                    isSearch:false,
                    crumbs: [],
                    companyName: "",
                    isUse:false               //确认发送按钮  点击后禁用  避免连续点击重复发送
                }
            },
            filters: {
               getData:function (input) {
                   var d = new Date(input);
                   var hour = d.getHours();
                   var minutes = d.getMinutes();
                   return  hour + ':' + minutes;
               },
               getHName: function(input) {
                 if(input != null){
                     return input.slice(-2);
                 }else{
                     return '';
                 }
               }
            },
            methods: {
                back: function () {
                    var name = "back";
                    sendInfoToJava(name);
                },
                exit: function () {
                    var name = "close";
                    sendInfoToJava(name);
                },
                getNow: function() {
                    var d = new Date();
                    var year = d.getFullYear();
                    var month = d.getMonth() + 1;
                    var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
                    var hour = d.getHours();
                    var minutes = d.getMinutes();
                    var seconds = d.getSeconds();
                    return year+ '-' + zeroize(month) + '-' + zeroize(day) + ' ' + zeroize(hour) + ':' + zeroize(minutes)
                },
                submit: function () {
    //
                    this.companyId = Request.cid;
                    this.createUser = Request.loginName;
                    var this_obj = this;
                    //alert(this_obj.time);
                    if(this_obj.title == ""){
                        mui.toast('请输入标题');
                    }else if(this_obj.message == ""){
                        mui.toast('请输入内容');
                    }/*else if(this_obj.selected.length==0){
                         mui.toast('请指定抄送人');
                    }*/else if(typeof(time)=="undefined"){
                        mui.toast('请指定截至时间');
                    }else if(this_obj.onlyOneArr.length==0){
                        mui.toast('请指定协作人');
                    }else{
                        var url = "/schedule/zgSchedule/add";
                        var data = {
                            "title":this_obj.title,
                            "message":this_obj.message,
                            "sendUser":'',
                            "ccUserName":[]
                        };

                        data.sendUser=this_obj.onlyOneArr[0].loginName;

                        if(this_obj.selected.length!=0){
                            for(var i=0;i<this_obj.selected.length;i++){
                                data.ccUserName.push(this_obj.selected[i].loginName);
                            }
                        }

                        data.createUser =this_obj.createUser;
                        data.companyId =this_obj.companyId;

                        var now = this_obj.getNow();
                        var timestamp2 = Date.parse(new Date(time));
                        timestamp2 = timestamp2 / 1000;
                        var timestamp1 = Date.parse(new Date(now));
                        timestamp1 = timestamp1 / 1000;
                        console.log(timestamp2);
                        console.log(timestamp1);
                        if(timestamp2 < timestamp1 || time == undefined){
                            data.deadlineTime = now + ":00";
                        }else{
                            data.deadlineTime=time+":00";
                        }
                        if(typeof(Request.cid) == "undefined" || typeof(Request.loginName) == "undefined"){
                            mui.toast("请重新登录");
                        }else{
                            common_ajaxs(url, data, function (res) {
                                alert("请求成功");
                                this_obj.isUse=true;
                                this_obj.back();
                            }, function (res) {
                                alert("请求失败");
                            });
                        }
                    }
                },
                save_time: function () {
                    var this_obj = this;
                    var date = new Date();
                    this_obj.time = date;
                    return date;
                },
                search: function() {
                     var this_obj = this;
                     this_obj.isSearch = true;
                     var url = "/user/selectUserByCompanyId";
                     var postData = {
                         "companyId": Request.cid,//必須傳
                         "name": this_obj.searchInfo
                    }
                    if(typeof(Request.cid) == "undefined"){
                        mui.toast("请重新登录");
                    }else{
                        common_ajax(url,postData,function (res) {
                           this_obj.depts = res.obj;
                           if(this_obj.selectOnly){
                               for(var i in this_obj.onlyOneArr){
                                   for(var j in this_obj.depts){
                                       if(this_obj.onlyOneArr[i]['loginName'] == this_obj.depts[j]['loginName']){
                                           this_obj.depts[j]['check'] = true;
                                       }else{
                                           this_obj.depts[j]['check'] = false;
                                       }
                                   }
                               }
                           }else{
                               for(var i in this_obj.selected){
                                  for(var j in this_obj.depts){
                                      if(this_obj.selected[i]['loginName'] == this_obj.depts[j]['loginName']){
                                          this_obj.depts[j]['check'] = true;
                                      }else if(!this_obj.depts[j]['check']){
                                          this_obj.depts[j]['check'] = false;
                                      }
                                  }
                              }
                           }
                        },function (res) {
                            mui.toast(res.errorMg);
                        });
                    }
                },
                 open_popup: function (msg) {
                    var this_obj = this;
                    this_obj.dept = true;
                    var number = 0;
                    this_obj.rid.push(number);
                    if(msg == 2){
                        this_obj.selectOnly = true;
                    }else{
                        this_obj.selectOnly = false;
                    }
                    show_depts(number,msg);
                },
                judge: function (index,msg) {
                    var this_obj = this;
                    if(this_obj.depts[index]['show'] == true){
                        this_obj.show_next(index,msg);
                    }else{
                        if(msg == 1){
                            this_obj.toggle(index);
                        }else{
                            this_obj.toggle_only(index);
                        }
                    }
                },
                show_next: function (index,msg) {
                    var this_obj = this;
                    if(this_obj.depts[index]['sel_able'] == true){
                        var number = this_obj.depts[index]['id'];
                        var name = this_obj.depts[index]['name'];
                        this_obj.crumbs.push({id: number,name: name});
                        this_obj.rid.push(number);
                        show_depts(number,msg);
                    }
                },
                toggle: function (index) {
                    var this_obj = this;
                    var url = "/organization/dept";
                    if(this_obj.depts[index]['check']==null || this_obj.depts[index]['check'] == false){
                        this_obj.depts[index]['check'] = true;
                        this_obj.depts[index]['sel_able'] = false;
                        if(this_obj.depts[index]['show'] == true){
                            recursion(this_obj.depts[index]['id']);
                        }else{
                            if(this_obj.creatUserName == this_obj.depts[index]['loginName']){
                                mui.toast("自己不能为抄送人");
                                this_obj.depts[index]['check'] = false;
                            }else{
                                if(this_obj.onlyOneArr.length > 0){
                                    for(var i in this_obj.onlyOneArr){
                                        if(this_obj.onlyOneArr[i]['loginName'] != this_obj.depts[index]['loginName']){
                                            this_obj.selected.push({id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});

                                        }else{
                                            this_obj.depts[index]['check'] = false;
                                            mui.toast(this_obj.depts[index]['name'] +"已经为指定协作人!");
                                        }
                                    }
                                }else{
                                    this_obj.selected.push({id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});

                                }
                            }
                            this_obj.selected_num = this_obj.selected.length;
                        }
                    }else{
                        this_obj.depts[index]['check'] = false;
                        this_obj.depts[index]['sel_able'] = true;
                        if(this_obj.depts[index]['show'] == true){
                            del(this_obj.depts[index]['id']);
                            function del(deptId) {
                                var data = {
                                    "companyId":Request.cid, //公司ID
                                    "deptId": deptId
                                };
                                if(typeof(Request.cid) == "undefined"){
                                    mui.toast("请重新登录");
                                }else {
                                    common_ajax(url, data, function (res) {
                                        if (res.code < 0) {
                                            mui.toast(res.errorMsg);
                                        } else {
                                            console.log(res);
                                            if (res.obj.depts.length == 0) {
                                                for (var j in res.obj.users) {
                                                    for (var y = 0; y < this_obj.selected.length; y++) {
                                                        if (this_obj.selected[y]['id'] == res.obj.users[j]['id']) {
                                                            this_obj.selected.splice(y, 1);
                                                            y--;
                                                        }
                                                    }
                                                }
                                            } else {
                                                for (var i in res.obj.depts) {
                                                    del(res.obj.depts[i]['id']);
                                                }
                                                for (var x in res.obj.users) {
                                                    for (var a = 0; a < this_obj.selected.length; a++) {
                                                        if (this_obj.selected[a]['id'] == res.obj.users[x]['id']) {
                                                            this_obj.selected.splice(a, 1);
                                                            a--;
                                                        }
                                                    }
                                                }
                                            }
                                            console.log(this_obj.selected);
                                            this_obj.selected_num = this_obj.selected.length;
                                        }
                                    }, function (res) {
                                        mui.toast("拉取联系人失败");
                                    });
                                }
                            }
                        }else{
                            for(var a = 0;a < this_obj.selected.length;a++){
                                if(this_obj.selected[a]['id'] == this_obj.depts[index]['id']){
                                    this_obj.selected.splice(a,1);
                                    a--;
                                }
                            }
                            this_obj.selected_num = this_obj.selected.length;
                        }
                    }
                },
                toggle_only: function(index) {
                    var this_obj = this;
                    var url = "/organization/dept";
                    if(this_obj.depts[index]['check']){
                        this_obj.depts[index]['check'] = false;
                        this_obj.onlyOneArr.splice(0,1)
                    }else{
                        for(var i in this_obj.depts){
                            this_obj.depts[i]['check'] = false;
                        }
                        this_obj.depts[index]['check'] = true;
                        this_obj.onlyOneArr.splice(0,1,{id: this_obj.depts[index]['id'],hName: this_obj.depts[index]['hName'],name: this_obj.depts[index]['name'],loginName: this_obj.depts[index]['loginName']});
                    }
                },
                confirm: function () {
                    var this_obj = this;
                    this_obj.searchInfo ="";
                    this_obj.dept = false;
                    this_obj.isSearch = false;
                    if(this_obj.selectOnly){
                        this_obj.selectOnly = false;
                        if(this_obj.selected.length > 0 ){
                            for(var i in this_obj.selected){
                                if(this_obj.onlyOneArr.length > 0 && this_obj.selected[i]['id'] == this_obj.onlyOneArr[0]['id']){
                                    mui.toast(this_obj.onlyOneArr[0]['name']+"已经为抄送人");
                                    this_obj.onlyOneArr = [];
                                }
                            }
                        }
                        if(this_obj.onlyOneArr.length > 0 && this_obj.onlyOneArr[0]['loginName'] == this_obj.creatUserName){
                            mui.toast("自己不能为指定协作人!");
                            this_obj.onlyOneArr = [];
                        }
                    }else{
                         var arr = unique(this_obj.selected);
                         this_obj.selected = arr.concat();
                     }
                },
                del: function (index) {
                    var this_obj = this;
                    this_obj.selected.splice(index, 1);
                },
                del_c:function(index) {
                  var this_obj = this;
                  this_obj.onlyOneArr.splice(index, 1);
                },
                go_back: function () {
                    var this_obj = this;
                    console.log(this_obj.rid);
                    if(this_obj.isSearch){
                        this_obj.isSearch = false;
                        this_obj.searchInfo = "";
                        if(this_obj.selectOnly){
                            show_depts(this_obj.rid[this_obj.rid.length-1],2);
                        }else{
                            show_depts(this_obj.rid[this_obj.rid.length-1],1);
                        }
                    }else{
                        if(this_obj.rid[this_obj.rid.length-1] == 0){
                            var btnArray = ['取消', '确定'];
                            mui.confirm('确定要取消本次操作吗？', '', btnArray, function(e) {
                                if (e.index == 1) {
                                    this_obj.dept = false;
                                    this_obj.selectOnly = false;
                                    this_obj.rid = [];
                                    this_obj.selected = [];
                                    this_obj.selected_num = this_obj.selected.length;
                                }
                            })
                        }else{
                            this_obj.crumbs.pop();
                            this_obj.rid.pop();
                            console.log(this_obj.rid);
                            var number = this_obj.rid[this_obj.rid.length-1];
                            if(this_obj.selectOnly){
                                show_depts(number,2);
                            }else{
                                show_depts(number,1);
                            }
                        }
                    }
                },
                show_info: function (flag, index) {
                    var this_obj = this;
                    if(flag == true){
                        if(this_obj.selectOnly){
                            show_depts(index,2);
                        }else{
                            show_depts(index,1);
                        }
                        this_obj.crumbs = [];
                    }else{
                        var number = this_obj.crumbs[index]['id'];
                        if(this_obj.selectOnly){
                           show_depts(number,2);
                       }else{
                           show_depts(number,1);
                       }
                        var len = this_obj.crumbs.length - (index + 1);
                        this_obj.crumbs.splice(index + 1, len);
                    }
                }
            }
        });
        app.save_time();
        setInterval(app.save_time(),20*1000);
        function unique(arr) {
            var result = [], isRepeated;
            for (var i = 0; i < arr.length; i++) {
                isRepeated = false;
                for (var j = 0; j < result.length; j++) {
                    if (arr[i]['loginName'] == result[j]['loginName']) {
                        isRepeated = true;
                        mui.toast("您已经选择了"+arr[i].name);
                        break;
                    }
                }
                if (!isRepeated) {
                    result.push(arr[i]);
                }
            }
            return result;
        }
        //已经选好的的添加到当前数组中
        function arrayIntersection(a, b){
            for(var i=0;i<b.length;i++){
                for(var j=0;j<a.length;j++){
                    if(b[i].id==a[j].id){
                        b[i].check=true;
                        break;
                    }
                }
            }
        }
        //下一级全选则本级显示选中
        function check_group(department,select){
            var all_select=0;
            for(var i=0;i<department.length;i++){
                var now_select=0;
                for(var j=0;j<select.length;j++){
                    if(department[i].id==select[j].id){
                        now_select=1;
                        break;
                    }
                }
                if(now_select!=1)break;
                if(i==department.length-1){
                    all_select=1;
                }
            }
            return all_select;
        }
        function show_depts(deptId,msg) {
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId // 初始传入0
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {
                        console.log(res);
                        app.depts = [];
                        for (var i in res.obj.depts) {
                            var hName = res.obj.depts[i]['name'].substr(0, 2);
                            app.depts.push({
                                id: res.obj.depts[i]['id'],
                                total: res.obj.depts[i]['total'],
                                hName: hName,
                                name: res.obj.depts[i]['name'],
                                check: false,
                                show: true,
                                sel_able: true
                            });
                            check_recursion(app.depts[i]['id'], i);
                        }
                        for (var j in res.obj.users) {
                            var eName = res.obj.users[j]['name'].slice(-2);
                            app.depts.push({
                                id: res.obj.users[j]['id'],
                                total: res.obj.users[j]['total'],
                                hName: eName,
                                name: res.obj.users[j]['name'],
                                check: false,
                                show: false,
                                sel_able: true,
                                loginName: res.obj.users[j]['loginName']
                            });
                        }
                        if (msg == 1) {
                            arrayIntersection(app.selected, app.depts);
                        } else if (msg == 2) {
                            arrayIntersection(app.onlyOneArr, app.depts);
                        }
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                })
            }
        }
        function check_recursion(deptId, index) {
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {
                        app.select_person = [];
                        if (res.obj.depts.length == 0) {
                            for (var j in res.obj.users) {
                                var eName = res.obj.users[j]['name'].slice(-2);
                                app.select_person.push({
                                    id: res.obj.users[j]['id'],
                                    hName: eName,
                                    name: res.obj.users[j]['name'],
                                    loginName: res.obj.users[j]['loginName']
                                });
                            }
                        } else {
                            for (var i in res.obj.depts) {
                                check_recursion(res.obj.depts[i]['id']);
                            }
                            for (var x in res.obj.users) {
                                var myName = res.obj.users[x]['name'].slice(-2);
                                app.select_person.push({
                                    id: res.obj.users[x]['id'],
                                    hName: myName,
                                    name: res.obj.users[x]['name'],
                                    loginName: res.obj.users[x]['loginName']
                                });
                            }
                        }
                        if (check_group(app.select_person, app.selected) == 1) {
                            app.depts[index]['check'] = true;
                        }
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                });
            }
        }
        function recursion(deptId) {
            var url = "/organization/dept";
            var data = {
                "companyId": Request.cid, //公司ID
                "deptId": deptId
            };
            if(typeof(Request.cid) == "undefined"){
                mui.toast("请重新登录");
            }else {
                common_ajax(url, data, function (res) {
                    if (res.code < 0) {
                        mui.toast(res.errorMsg);
                    } else {
                        console.log(res);
                        if (res.obj.depts.length == 0) {
                            for (var j in res.obj.users) {
                                var eName = res.obj.users[j]['name'].slice(-2);
                                if (app.creatUserName == res.obj.users[j]['loginName']) {
                                    mui.toast("自己不能为抄送人")
                                    for (var i in app.depts) {
                                        if (app.depts[i]['id'] == deptId) {
                                            app.depts[i]['check'] = false
                                        }
                                    }
                                } else {
                                    if (app.onlyOneArr.length > 0) {
                                        for (var i in app.onlyOneArr) {
                                            if (app.onlyOneArr[0]['id'] != res.obj.users[i]['id']) {
                                                app.selected.push({
                                                    id: res.obj.users[j]['id'],
                                                    hName: eName,
                                                    name: res.obj.users[j]['name'],
                                                    loginName: res.obj.users[j]['loginName']
                                                });
                                            } else {
                                                mui.toast(res.obj.users[j]['name'] + "已经为指定协作人!");
                                                for (var i in app.depts) {
                                                    if (app.depts[i]['id'] == deptId) {
                                                        app.depts[i]['check'] = false
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        app.selected.push({
                                            id: res.obj.users[j]['id'],
                                            hName: eName,
                                            name: res.obj.users[j]['name'],
                                            loginName: res.obj.users[j]['loginName']
                                        });
                                    }

                                }
                                //                            app.selected.push({id: res.obj.users[j]['id'],hName: eName,name: res.obj.users[j]['name'],loginName: res.obj.users[j]['loginName']});
                            }
                        } else {
                            for (var x in res.obj.users) {
                                var myName = res.obj.users[x]['name'].slice(-2);
                                //                            app.selected.push({id: res.obj.users[x]['id'],hName: myName,name: res.obj.users[x]['name'],loginName: res.obj.users[x]['loginName']});
                                if (app.creatUserName == res.obj.users[x]['loginName']) {
                                    mui.toast("自己不能为抄送人")
                                    for (var i in app.depts) {
                                        if (app.depts[i]['id'] == deptId) {
                                            app.depts[i]['check'] = false
                                        }
                                    }
                                } else {
                                    if (app.onlyOneArr.length > 0) {
                                        for (var i in app.onlyOneArr) {
                                            if (app.onlyOneArr[0]['id'] != res.obj.users[i]['id']) {
                                                app.selected.push({
                                                    id: res.obj.users[x]['id'],
                                                    hName: myName,
                                                    name: res.obj.users[x]['name'],
                                                    loginName: res.obj.users[x]['loginName']
                                                });
                                            } else {
                                                mui.toast(res.obj.users[x]['name'] + "已经为指定协作人!");
                                                for (var i in app.depts) {
                                                    if (app.depts[i]['id'] == deptId) {
                                                        app.depts[i]['check'] = false
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        app.selected.push({
                                            id: res.obj.users[x]['id'],
                                            hName: myName,
                                            name: res.obj.users[x]['name'],
                                            loginName: res.obj.users[x]['loginName']
                                        });
                                    }
                                }
                            }
                            for (var i in res.obj.depts) {
                                recursion(res.obj.depts[i]['id']);
                            }
                        }
                        console.log(app.selected);
                        app.selected_num = app.selected.length;
                    }
                }, function (res) {
                    mui.toast("拉取联系人失败");
                });
            }
        }
        function sendInfoToJava(name) {
            //调用android程序中的方法，并传递参数
            javascript:WebView.showInfoFromJs(name);
        }

        var name = "companyId"+Request.cid;
        if(typeof(Request.cid) == "undefined"){
            mui.toast("请重新登录");
        }else {
            sendInfoToJava(name);
        }

        //在android代码中调用此方法
        function showInfoFromJava(msg){
            app.companyName = msg;
        }

    (function($) {
        $.init();
        var results = $('#results')[0];
        var btns = $('.btn');
        btns.each(function(i, btn) {
            btn.addEventListener('tap', function() {
                var _self = this;
                if(_self.picker) {
                    _self.picker.show(function (rs) {
                        if(typeof(rs.text) == "undefined"){
                            var d = new Date();
                            var year = d.getFullYear();
                            var month = d.getMonth() + 1;
                            var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
                            var hour = d.getHours();
                            var minutes = d.getMinutes();
                            var seconds = d.getSeconds();
                            results.innerText = year+ '-' + zeroize(month) + '-' + zeroize(day) + ' ' + zeroize(hour) + ':' + zeroize(minutes);
                        }else{
                            results.innerText = rs.text;
                        }
                        _self.picker.dispose();
                        _self.picker = null;
                    });
                } else {
                    var optionsJson = this.getAttribute('data-options') || '{}';
                    var options = JSON.parse(optionsJson);
                    var id = this.getAttribute('id');
                    /*
                     * 首次显示时实例化组件
                     * 示例为了简洁，将 options 放在了按钮的 dom 上
                     * 也可以直接通过代码声明 optinos 用于实例化 DtPicker
                     */
                    _self.picker = new $.DtPicker(options);
                    _self.picker.show(function(rs) {
                        /*
                         * rs.value 拼合后的 valuef
                         * rs.text 拼合后的 text
                         * rs.y 年，可以通过 rs.y.vaue 和 rs.y.text 获取值和文本
                         * rs.m 月，用法同年
                         * rs.d 日，用法同年
                         * rs.h 时，用法同年
                         * rs.i 分（minutes 的第二个字母），用法同年
                         */
                        rs.text=rs.y.text+"-"+rs.m.text+"-"+rs.d.text+"  "+rs.h.text+":"+rs.i.text;
                        if(typeof(rs.text) == "undefined"){
                            var d = new Date();
                            var year = d.getFullYear();
                            var month = d.getMonth() + 1;
                            var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
                            var hour = d.getHours();
                            var minutes = d.getMinutes();
                            var seconds = d.getSeconds();
                            time = year+ '-' + zeroize(month) + '-' + zeroize(day) + ' ' + zeroize(hour) + ':' + zeroize(minutes);
                            results.innerText = year+ '-' + zeroize(month) + '-' + zeroize(day) + ' ' + zeroize(hour) + ':' + zeroize(minutes);
                        }else{
                            time = rs.text;
                            results.innerText = rs.text;
                        }
                        /*
                         * 返回 false 可以阻止选择框的关闭
                         * return false;
                         */
                        /*
                         * 释放组件资源，释放后将将不能再操作组件
                         * 通常情况下，不需要示放组件，new DtPicker(options) 后，可以一直使用。
                         * 当前示例，因为内容较多，如不进行资原释放，在某些设备上会较慢。
                         * 所以每次用完便立即调用 dispose 进行释放，下次用时再创建新实例。
                         */
                        _self.picker.dispose();
                        _self.picker = null;
                    });
                }

            }, false);
        });
    })(mui);
</script>
</html>